package com.seelyn.rhea.mvc.internal;

import com.seelyn.rhea.mvc.context.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author slacrey
 * @since 2016/1/24
 */
public class InternalResponse implements Response {
    /**
     * The LOG
     */
    private static final Logger LOG = LoggerFactory.getLogger(InternalResponse.class);

    private final HttpServletResponse response;
    private String body;

    public static Response build(HttpServletResponse response) {
        return new InternalResponse(response);
    }

    InternalResponse(HttpServletResponse response) {
        this.response = response;
    }


    @Override
    public void status(int statusCode) {
        response.setStatus(statusCode);
    }

    @Override
    public void type(String contentType) {
        response.setContentType(contentType);
    }

    @Override
    public void body(String body) {
        this.body = body;
    }

    @Override
    public String body() {
        return body;
    }

    @Override
    public HttpServletResponse raw() {
        return response;
    }

    @Override
    public void redirect(String location) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Redirecting ({} {} to {}", "Found", HttpServletResponse.SC_FOUND,
                    location);
        }
        try {
            response.sendRedirect(location);
        } catch (IOException ioException) {
            LOG.warn("Redirect failure", ioException);
        }
    }

    @Override
    public void redirect(String location, int httpStatusCode) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Redirecting ({} to {}", httpStatusCode, location);
        }
        response.setStatus(httpStatusCode);
        response.setHeader("Location", location);
        response.setHeader("Connection", "close");
        try {
            response.sendError(httpStatusCode);
        } catch (IOException e) {
            LOG.warn("Exception when trying to redirect permanently", e);
        }
    }

    @Override
    public void header(String header, String value) {
        response.addHeader(header, value);
    }

    @Override
    public void cookie(String name, String value) {
        cookie(name, value, -1, false);
    }

    @Override
    public void cookie(String name, String value, int maxAge) {
        cookie(name, value, maxAge, false);
    }

    @Override
    public void cookie(String name, String value, int maxAge, boolean secured) {
        cookie(name, value, maxAge, secured, false);
    }

    @Override
    public void cookie(String name, String value, int maxAge, boolean secured, boolean httpOnly) {
        cookie("", name, value, maxAge, secured, httpOnly);
    }

    @Override
    public void cookie(String path, String name, String value, int maxAge, boolean secured) {
        cookie(path, name, value, maxAge, secured, false);
    }

    @Override
    public void cookie(String path, String name, String value,
            int maxAge, boolean secured, boolean httpOnly) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath(path);
        cookie.setMaxAge(maxAge);
        cookie.setSecure(secured);
        cookie.setHttpOnly(httpOnly);
        response.addCookie(cookie);
    }

    @Override
    public void removeCookie(String name) {
        Cookie cookie = new Cookie(name, "");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}
